package com.zjx.eduorder.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjx.commonutils.JwtUtils;
import com.zjx.commonutils.R;
import com.zjx.eduorder.entity.Order;
import com.zjx.eduorder.service.OrderService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *
 * 订单 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2025-02-22
 */
@RestController
@RequestMapping("/eduorder/order")
//@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService;

    //1 生成订单的方法
    @PostMapping("createOrder/{courseId}")
    public R saveOrder(@PathVariable String courseId, HttpServletRequest request){
        //创建订单，返回订单号
        String orderNo = orderService.createOrders(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return R.ok().data("orderId",orderNo);
    }

    //2 根据订单id查询订单信息
    @GetMapping("getOrderInfo/{orderId}")
    public R getOrderInfo(@PathVariable String orderId){
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",orderId);
        Order order = orderService.getOne(wrapper);
        return R.ok().data("item",order);
    }

    //根据课程id和用户id查询订单表中订单状态
    @GetMapping("isBuyCourse/{courseId}/{memberId}")
    public boolean isBuyCourse(@PathVariable String courseId,@PathVariable String memberId){

        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.eq("member_id",memberId);
        wrapper.eq("status",1);//支付状态 1代表已经支付
        int count = orderService.count(wrapper);
        if (count>0){//已经支付
            return true;
        }else {
            return false;
        }
    }

    @ApiOperation(value = "订单列表")
    @GetMapping("{page}/{limit}")
    public R pageQuery(
            @ApiParam(name = "page", value = "当前页面", required = true) @PathVariable Long page, @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Long limit,
            @ApiParam(name = "order", value = "查询对象", required = false)  Order order, HttpServletRequest request){
        Page<Order> pageParam= new Page<>(page,limit);
        orderService.pageQuery(pageParam,order);
        List<Order> records = pageParam.getRecords();

        long total = pageParam.getTotal();
        return R.ok().data("total",total).data("rows",records);
    }

    @ApiOperation(value = "逻辑删除订单")
    @DeleteMapping("{id}")
    public R delete(
            @ApiParam(name = "id", value = "id", required = true) @PathVariable String id){
        orderService.removeById(id);
        return R.ok().message("删除成功");
    }
    //获取当前用户订单列表
    @ApiOperation(value = "获取当前用户订单列表")
    @GetMapping("orderList")
    public R list(HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        List<Order> list = orderService.selectByMemberId(memberId);
        return R.ok().data("items", list);
    }

    //查询订单表中订单的状态 判断课程是否购买
    @ApiOperation( "判断课程是否购买")
    @GetMapping("isBought/{courseId}")
    public R isBuyByCourseId(@PathVariable String courseId, HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        Boolean isBought = orderService.isBoughtByCourseId(courseId, memberId);
        return R.ok().data("isBought", isBought);
    }

    @ApiOperation(value = "删除订单")
    @DeleteMapping("removeOrder/{orderId}")
    public R remove(@PathVariable String orderId, HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        boolean result = orderService.removeById(orderId, memberId);
        if(result){
            return R.ok().message("删除订单成功");
        }else{
            return R.error().message("数据不存在");
        }
    }
}

